/**
 * KopsoxFramework
 * Copyright (C) 2010 Free Software Foundation, Inc. <http://fsf.org/>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.general.priority.impl;

import java.util.Comparator;

import org.kopsox.general.priority.LoggingPriority;
import org.kopsox.general.priority.LoggingPriorityEnum;
import org.kopsox.general.priority.Priority;

/**
 * @author Konrad Renner - 28.08.2010
 *
 */
public class LoggingPriorityImpl implements LoggingPriority {

	private static final long serialVersionUID = -6826247027418158690L;
	private int priorityValue;
	private transient Comparator<Priority> comparator = null;
	
	public LoggingPriorityImpl(int priorityValue){
		this(priorityValue,null);
	}
	
	public LoggingPriorityImpl(int priorityValue,Comparator<Priority> comparator){
		this.priorityValue = priorityValue;
		this.comparator = comparator;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.general.priority.Priority#getPriorityValue()
	 */
	@Override
	public int getPriorityValue() {
		return this.priorityValue;
	}

	/* (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(final Priority arg0) {
		int returnCode;
		
		if(this.comparator != null){
			returnCode = this.comparator.compare(this, arg0);
		}else{
			returnCode = compareWithoutComparator(arg0);
		}
		
		return returnCode;
	}

	private final int compareWithoutComparator(final Priority priority){
		int sum = this.priorityValue - priority.getPriorityValue();
		
		int returnCode = 0;
		if(sum > 0)
			returnCode = -1;
		else if(sum < 0)
			returnCode = 1;
		
		return returnCode;
	}
	
	

	@Override
	public Comparator<Priority> getComparator() {
		return this.comparator;
	}

	@Override
	public void setComparator(Comparator<Priority> comparator) {
		this.comparator = comparator;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + this.priorityValue;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		LoggingPriorityImpl other = (LoggingPriorityImpl) obj;
		if (priorityValue != other.priorityValue)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return LoggingPriorityEnum.getPriorityValueAsString(this);
	}
}
